// File:    TnQueryFilter.cs
// Author:  HT
// Created: Monday, November 28, 2011 9:50:29 AM
// Purpose: Definition of Class TnQueryFilter

using System;
using System.Data.SqlClient;

namespace Tayninh2011
{
   public class TnQueryFilter : ITnQueryFilter
   {
       SqlDataReader reader = null;
       SqlCommand command = null;
       String strSql = String.Empty;
       System.Data.CommandBehavior behavior=System.Data.CommandBehavior.SingleResult;
       SqlConnection sqlConn = null;

       public TnQueryFilter() { }
       public TnQueryFilter(SqlConnection sql_connection, String table, String[] field_name, String dieukien,System.Data.CommandBehavior type_read, int gioihan)
       {
           strSql = "select * from " + table;
           if (dieukien != "")
           {
               strSql += " where " + dieukien;
           }
           behavior = type_read;
           command = new SqlCommand(strSql, sql_connection);
       }
       public TnQueryFilter(SqlConnection sql_connection, String table, String field_name, String dieukien, System.Data.CommandBehavior type_read, int gioihan)
       {
           strSql = "select "+field_name+" from " + table;
           if (dieukien != "")
           {
               strSql += " where " + dieukien;
           }
           behavior = type_read;
           sqlConn=sql_connection;
           sqlConn.Open();
           command = new SqlCommand(strSql, sql_connection);
       }

       public TnQueryFilter(SqlConnection sql_connection, String table, String dieukien, System.Data.CommandBehavior type_read, int gioihan, params string[] field_name)
       {
           strSql = "select " + TnUtilities.ArrayToString(field_name) + " from " + table;
           if (dieukien != "")
           {
               strSql += " where " + dieukien;
           }
           behavior = type_read;
           sqlConn = sql_connection;
           sqlConn.Open();
           command = new SqlCommand(strSql, sql_connection);
       }
       #region ITnQueryFilter Members
       
       void ITnQueryFilter.Excute()
       {
           reader = command.ExecuteReader(behavior);
       }

       void ITnQueryFilter.Close()
       {
           this.reader.Close();
           sqlConn.Close();
       }

       SqlDataReader ITnQueryFilter.GetResultCursor()
       {
           
           return reader;
       }

       void ITnQueryFilter.ExcuteNonQuery()
       {
           if (!reader.IsClosed)
           {
               command.ExecuteNonQuery();
           }
       }

       #endregion
   }
}
